#coding:utf-8
#file:getimg.py

import Tkinter
import urllib
import HTMLParser
class MyHTMLParser(HTMLParser.HTMLParser):
    def __init__(self):
        HTMLParser.HTMLParser.__init__(self)
        self.gifs = []
        self.jpgs = []

    def handle_starttag(self, tags, attrs):
        t = [v for k, v in attrs if tags=='img']
        if 'jpg' in t:
            self.jpgs.append(t)
        else:
            pass

        '''
        if tags == 'img':
            for attr in attrs:
                for t in attr:
                    if 'git' in t:
                        self.gifs.append(t)
                    elif 'jpg' in t:
                        self.jpgs.append(t)
                    else:
                        pass
        '''

    def get_gifs(self):
        return self.gifs
    def get_jpgs(self):
        return self.jpgs

class Window:
    def __init__(self, root):
        self.root = root
        self.label = Tkinter.Label(root, text = '输入URL:')
        self.label.place(x=5, y=15)
        self.entryUrl = Tkinter.Entry(root, width = 30)
        self.entryUrl.place(x=65, y=15)
        self.get = Tkinter.Button(root,
                                  text='获取图片', command=self.Get)
        self.get.place(x=280, y=15)
        self.edit = Tkinter.Text(root, width=470, height=600)
        self.edit.place(y=50)

    def Get(self):
        url = self.entryUrl.get()
        page = urllib.urlopen(url)
        data = page.read()
        parser = MyHTMLParser()
        parser.feed(data)
        self.edit.insert(Tkinter.END, '===GIF===\n')
        gifs = parser.get_gifs()
        for gif in gifs:
            self.edit.insert(Tkinter.END, gif+'\n')
        self.edit.insert(Tkinter.END, '==========\n')
        self.edit.insert(Tkinter.END, '===jpg====\n')
        jpgs = parser.get_jpgs()
        for jpg in jpgs:
            self.edit.insert(Tkinter.END, jpg+'\n')
        self.edit.insert(Tkinter.END, '==========\n')

        page.close()

if __name__ == '__main__':
    root = Tkinter.Tk()
    window = Window(root)
    root.minsize(600, 480)
    root.mainloop()
        
